//模块接口
var init = {};
init.styles = {};
init.views = {};
init.controls = {};

//css
init.styles.bg = "width:100%; height:100vh; background:rgba(80,80,80,0.3); position:fixed; top:0; left:0;";
init.styles.wait = "width:160px; height:40px; position:fixed; top:50%; left:50%; margin-left:-80px; margin-top:-20px; background:white; text-align:center; line-height:40px; box-shadow:0 0 5px #999999; border:solid 1px silver; user-select:none;";
init.styles.box = "padding:0; margin:0; width:300px; position:fixed; top:20%; left:50%; margin-left:-150px; background:white; text-align:center;  box-shadow:0 0 5px #999999; border:solid 1px silver; user-select:none;";
init.styles.top = "padding:0 20px; height:30px; line-height:30px; background:#e2e2e2; color:#666666;";
init.styles.msg = "padding:10px 20px; line-height:30px; text-align:left;";
init.styles.bts = "width:100%; height:30px; display:flex;";
init.styles.bt1 = "border:0; line-height:30px; cursor:pointer; background:#b4b4b4; flex:1;";
init.styles.bt2 = "border:0; line-height:30px; cursor:pointer; background:#d2d2d2; flex:1;";

init.controls.ShowGlobal = function (cb) {
    if (!document.querySelector(".global_alert")) {
        var page = document.createElement("div");
        page.setAttribute("class", "global_alert");
        page.setAttribute("style", init.styles.bg);
        document.body.appendChild(page);
    }
    document.querySelector(".global_alert").style.display = "block";
    cb();
}


init.views.waiting = {
    msg: "请等待(Waiting)",
    Show: function (msg) {
        init.views.waiting.msg = msg;
        init.controls.ShowGlobal(function () {
            m.render(document.querySelector(".global_alert"), m(init.views.waiting));
        });
    },
    view: function () {
        return m("div", { style: init.styles.wait }, init.views.waiting.msg)
    }
}

init.views.alert = {
    top: "错误提示(Error)",
    msg: "发生错误，无提示信息(An error occurred without prompt)",
    cb: "",
    Show: function (msg, top, cb) {
        console.log(top);
        init.views.alert.top = (typeof top != "undefined" ? top : "错误提示(Error)");
        init.views.alert.msg = (typeof msg != "undefined" ? msg : "发生错误，无提示信息(An error occurred without prompt)");
        init.views.alert.cb = (typeof cb != "undefined" ? cb : "");
        init.controls.ShowGlobal(function () {
            m.render(document.querySelector(".global_alert"), m(init.views.alert));
        });
    },
    view: function () {
        return m("div", { style: init.styles.box }, [
            m("div", { style: init.styles.top }, init.views.alert.top),
            m("div", { style: init.styles.msg }, init.views.alert.msg),
            m("div", { style: init.styles.bts }, [
                m("button", {
                    style: init.styles.bt1, onclick: function () {
                        init.Hide();
                        if (init.views.alert.cb != "") {
                            //回调处理
                            init.views.alert.cb();
                        }
                    }
                }, "确定(Confirm)"),
                init.views.alert.top != "错误提示(Error)" ?
                    m("button", { style: init.styles.bt2, onclick: init.Hide }, "取消(Cancel)") : ""
            ])
        ])
    }
}

//接口
init.Hide = function () {
    document.querySelector(".global_alert").style.display = "none";
    m.render(document.querySelector(".global_alert"), "");
}
init.Waiting = function (msg) {
    init.views.waiting.Show(msg);
}
init.Error = function (msg, cb) {
    init.views.alert.Show(msg, "错误提示(Error)", cb);
}
init.Message = function (msg, cb) {
    init.views.alert.Show(msg, "提示信息(Message)", cb);
}
init.Confirm = function (msg, cb) {
    init.views.alert.Show(msg, "确认操作(Confirm)", cb);
}
//验证浏览器
init.CheckBrower = function () {
    var userAgent = navigator.userAgent;
    // console.log(userAgent);
    if (userAgent.indexOf("Opera") > -1) {
        console.log("Opera");
        return true
    }
    if (userAgent.indexOf("Firefox") > -1) {
        console.log("Firefox");
        return true
    }
    if (userAgent.indexOf("Chrome") > -1) {
        console.log("Chrome");
        return true
    }
    if (userAgent.indexOf("Edge") > -1) {
        console.log("Edge");
        return true
    }
    if (userAgent.indexOf("Safari") > -1) {
        console.log("Safari");
        return true
    }
    if (userAgent.indexOf("MSIE") > -1) {
        console.log("MSIE");
        document.write("<h1>IE10以下版本不支持该网站(This website is not supported in ie10 or below)</h1>");
        return false
    }
    return true
}
//20220318新增
//日期
init.GetDate = function (days) {
    var _d = new Date();//默认当天
    if (typeof days != "undefined") {
        if (days.substr(0, 1) == "+" || days.substr(0, 1) == "-") {
            _d = _d.valueOf() + 1000 * 3600 * 24 * parseInt(days);
        } else {
            console.log("init.GetDate(days),days格式错误(format error)");
            return;
        }
        _d = new Date(_d);
    }
    var year = _d.getFullYear();
    var month = _d.getMonth() + 1;
    var day = _d.getDate();
    if (month < 10) month = '0' + month;
    if (day < 10) day = '0' + day;
    return year + "-" + month + "-" + day;
}
//时间
init.GetTime = function (date) {
    // var timeZone = (new Date().getTimezoneOffset()) * 60 * 1000;
    // console.log(new Date().getTimezoneOffset());
    var _date = '';
    if (typeof date == "undefined") {
        _date = init.GetDate();
    } else {
        _date = date;
    }
    var _d = new Date();
    var hour = _d.getHours();
    var minute = _d.getMinutes();
    var second = _d.getSeconds();
    if (hour < 10) hour = '0' + hour;
    if (minute < 10) minute = '0' + minute;
    if (second < 10) second = '0' + second;
    return _date + " " + hour + ":" + minute + ":" + second;
}
//時區
init.SetFSJTime = function () {

    var _d = new Date();
    _d = _d.valueOf();
    //格林時間
    var gtime = _d + (new Date().getTimezoneOffset()) * 60 * 1000;

    //夏令時
    var defaultTimezone = 7;
    var summerTime = new Date(gtime);
    if (summerTime.getMonth() > 4 && summerTime.getMonth() < 10) {
        defaultTimezone = 6;
    }
    if (summerTime.getMonth() == 3 && summerTime.getDate() > 2) {
        defaultTimezone = 6;
    }
    if (summerTime.getMonth() == 9 && summerTime.getDate() < 30) {
        defaultTimezone = 6;
    }
    //時區轉換
    var time = gtime - defaultTimezone * 3600 * 1000;
    var _d = new Date(time);
    var year = _d.getFullYear();
    var month = _d.getMonth() + 1;
    var day = _d.getDate();
    var hour = _d.getHours();
    var minute = _d.getMinutes();
    var second = _d.getSeconds();
    if (month < 10) month = '0' + month;
    if (day < 10) day = '0' + day;
    if (hour < 10) hour = '0' + hour;
    if (minute < 10) minute = '0' + minute;
    if (second < 10) second = '0' + second;
    return "(FSJ)" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}